﻿using System.IO;
using 历史记录.BusinessLogicLayer;
using 历史记录.DataAccessLayer;

namespace 历史记录
{
    public static class HistoryFactory
    {
        public static readonly HistoryInfo Info = new HistoryInfo();

        /// <summary>
        /// 关闭所有记录任务线程
        /// </summary>
        public static void StopAll()
        {
            History.StopAll();
        }

        public static History<T> Creator<T>(string name, HistoryInfo info = null) where T : Model, new()
        {
            if (info == null) info = Info;
            var dal = CreatorDAL<T>(name, info);
            var bll = CreatorBLL(dal, info);
            return new History<T>(bll, info.DefaultLengh, info.DefaultTimeout);
        }

        private static IDAL<T> CreatorDAL<T>(string name, HistoryInfo info) where T : Model, new()
        {
            switch (info.DALType)
            {
                case DAL.LiteDBC:
                    return new LiteDBClient<T>(Path.Combine(info.ConnectionPath, name));

                case DAL.SqlIteSugar:
                default:
                    return new SugarClient<T>(Path.Combine(info.ConnectionPath, name));
            }
        }

        private static IBLL<T> CreatorBLL<T>(IDAL<T> dal, HistoryInfo info) where T : Model, new()
        {
            switch (info.BLLType)
            {
                case BLL.Single:
                    return new SingleBll<T>(dal, info.SaveSpan, info.DeleteSpan);

                case BLL.Submeter:
                default:
                    return new SubmeterBll<T>(dal, info.SaveSpan, info.DeleteSpan);
            }
        }
    }
}